// 新增
$(function () {
    $("#save-feedback-btn").click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $("#feedback-dialog");

        //获取元素
        var feedback_dateE = $("input[name='feedback_date']");
        var providerE = $("input[name='provider']");
        var issue_descE = $("textarea[name='issue_desc']");
        var reasonE = $("textarea[name='reason']");
        var solutionE = $("textarea[name='solution']");
        var ishandledE = $("#ishandled option:checked");
        // var issue_typeE = $("#issue_type"); //获取下拉菜单被选中的项，但是有问题：通过val()可以获取选中的value，但是通过text()会全部获取下拉的文本
        var issue_typeE = $("#issue_type option:checked"); //获取下拉菜单被选中的项，这种方式可以正常获取到选中的value和text
        var reciverE = $("input[name='reciver']");
        var resolverE = $("input[name='resolver']");
        var remarkE = $("textarea[name='remark']");
        var image_urlE = $("input[name='image_url']");
        var issue_leverE = $("#issue_lever option:checked");
        var isBugE = $("#isBug option:checked");
        var bug_urlE = $("input[name='bug_url']");
        var influenceE = $("textarea[name='influence']");

        // 获取元素的值
        var feedback_date = feedback_dateE.val();
        var provider = providerE.val();
        var issue_desc = issue_descE.val();
        var reason = reasonE.val();
        var solution = solutionE.val();
        var ishandled = ishandledE.val();
        var issue_type = issue_typeE.val(); //获取下拉被选中的文本
        var reciver = reciverE.val();
        var resolver = resolverE.val();
        var remark = remarkE.val();
        var image_url = image_urlE.val();
        var issue_lever = issue_leverE.val();
        var isBug = isBugE.val();
        var bug_url = bug_urlE.val();
        var influence = influenceE.val();

        // 遍历获取选中的业务线
        var obj = document.getElementsByName("business-box");
        var business_checked = [];
        for(var k in obj){
            if(obj[k].checked) {
                business_checked.push(obj[k].value);
            }
        }
        business_checked = String(business_checked);

        var submitType = self.attr("data-type"); //编辑的时候动态绑定该属性
        var feedbackId = self.attr("data-id"); //编辑的时候动态绑定该属性

        if(!feedback_date || !provider || !business_checked || !issue_desc || !ishandled || !issue_type || issue_lever < 0 || isBug < 0 || !reciver){
            amanalert.alertInfo("有必填项没填写，请填写完整后再提交！");
            return;
        }

        var d = new Date();
        var year = d.getFullYear();
        var month = change(d.getMonth()+1);
        var day = change(d.getDate());
        function change(t){
            if(t < 10){
                return "0" + t;
            }else{
                return t;
            }
        }
        var currentDate=year+'-'+month+'-'+day;

        if(feedback_date > currentDate){
            amanalert.alertInfo("反馈日期不能大于当前日期！");
            return;
        }

        var url = '';
        if(submitType == 'update'){
            url = '/updatefeedback/';
        }else {
            url = '/addfeedback/';
        }

        amanajax.post({
            'url':url,
            'data':{
                'feedback_date':feedback_date,
                'provider':provider,
                'issue_desc':issue_desc,
                'reason':reason,
                'solution':solution,
                'ishandled':ishandled,
                'issue_type':issue_type,
                'reciver':reciver,
                'resolver':resolver,
                'remark':remark,
                'image_url':image_url,
                'issue_lever':issue_lever,
                'isBug':isBug,
                'bug_url':bug_url,
                'influence':influence,
                'business':business_checked,
                'feedback_id':feedbackId
            },
            'success':function (data) {
                dialog.modal('hide');
                if(data['code'] == 1){
                    if(url.indexOf("updatefeedback") != -1){
                        amanalert.alertSuccessToast("修改成功！");
                    }else {
                        amanalert.alertSuccessToast("添加成功！");
                    }
                    window.location.reload();
                }else {
                    amanalert.alertInfo(data['message']);
                }
            },
            'fail':function (error) {
                amanalert.alertNetworkError();
            }
        });
    });
});

// 保存后继续添加
$(function () {
    $("#saveAndContinue-btn").click(function (event) {
        event.preventDefault();
        var self = $(this);
        //获取元素
        var feedback_dateE = $("input[name='feedback_date']");
        var providerE = $("input[name='provider']");
        var issue_descE = $("textarea[name='issue_desc']");
        var reasonE = $("textarea[name='reason']");
        var solutionE = $("textarea[name='solution']");
        var ishandledE = $("#ishandled option:checked");
        var issue_typeE = $("#issue_type option:checked"); //获取下拉菜单被选中的项，这种方式可以正常获取到选中的value和text
        var reciverE = $("input[name='reciver']");
        var resolverE = $("input[name='resolver']");
        var remarkE = $("textarea[name='remark']");
        var image_urlE = $("input[name='image_url']");
        var issue_leverE = $("#issue_lever option:checked");
        var isBugE = $("#isBug option:checked");
        var bug_urlE = $("input[name='bug_url']");
        var influenceE = $("textarea[name='influence']");

        // 获取元素的值
        var feedback_date = feedback_dateE.val();
        var provider = providerE.val();
        var issue_desc = issue_descE.val();
        var reason = reasonE.val();
        var solution = solutionE.val();
        var ishandled = ishandledE.val();
        var issue_type = issue_typeE.val(); //获取下拉被选中的文本
        var reciver = reciverE.val();
        var resolver = resolverE.val();
        var remark = remarkE.val();
        var image_url = image_urlE.val();
        var issue_lever = issue_leverE.val();
        var isBug = isBugE.val();
        var bug_url = bug_urlE.val();
        var influence = influenceE.val();

        // 遍历获取选中的业务线
        var obj = document.getElementsByName("business-box");
        var business_checked = [];
        for(var k in obj){
            if(obj[k].checked) {
                business_checked.push(obj[k].value);
            }
        }
        business_checked = String(business_checked);

        if(!feedback_date || !provider || !business_checked || !issue_desc || !ishandled || !issue_type || issue_lever < 0 || isBug < 0 || !reciver){
            amanalert.alertInfo("有必填项没填写，请填写完整后再提交！");
            return;
        }

        var d = new Date();
        var year = d.getFullYear();
        var month = change(d.getMonth()+1);
        var day = change(d.getDate());
        function change(t){
            if(t < 10){
                return "0" + t;
            }else{
                return t;
            }
        }
        var currentDate=year+'-'+month+'-'+day;

        if(feedback_date > currentDate){
            amanalert.alertInfo("反馈日期不能大于当前日期！");
            return;
        }

        amanajax.post({
            'url':'/addfeedback/',
            'data':{
                'feedback_date':feedback_date,
                'provider':provider,
                'issue_desc':issue_desc,
                'reason':reason,
                'solution':solution,
                'ishandled':ishandled,
                'issue_type':issue_type,
                'reciver':reciver,
                'resolver':resolver,
                'remark':remark,
                'image_url':image_url,
                'issue_lever':issue_lever,
                'isBug':isBug,
                'bug_url':bug_url,
                'influence':influence,
                'business':business_checked
            },
            'success':function (data) {
                if(data['code'] == 1){
                    amanalert.alertSuccessToast("添加成功！");
                    providerE.val("");
                    issue_descE.val("");
                    reasonE.val("");
                    solutionE.val("");
                    reciverE.val("");
                    resolverE.val("");
                    remarkE.val("");
                    image_urlE.val("");
                    bug_urlE.val("");
                    influenceE.val("");
                    $("[name=business-box]:checkbox").prop("checked", false);
                }else {
                    amanalert.alertInfo(data['message']);
                }
            },
            'fail':function (error) {
                amanalert.alertNetworkError();
            }
        });
    });
});

// 编辑
$(function () {
   $(".edit-feedback-btn").click(function (event) {
        var self = $(this);
        event.preventDefault();
        var dialog = $("#feedback-dialog");
        dialog.modal("show");
        // 编辑页面不显示保存并继续按钮
        var saveContinueBtn = dialog.find("#saveAndContinue-btn");
        saveContinueBtn.css("display", "none");

        var tr = self.parent().parent();

        // 回显date，由于date存储的是时间格式，需要截取前面的日期进行回显
        var feedback_date = String(tr.attr("data-feedback_date")).substring(0,10);
        var provider = tr.attr("data-provider");
        var issue_desc = tr.attr("data-issue_desc");
        var reason = tr.attr("data-reason");
        var solution = tr.attr("data-solution");
        var reciver = tr.attr("data-reciver");
        var resolver = tr.attr("data-resolver");
        var remark = tr.attr("data-remark");
        var image_url = tr.attr("data-image_url");
        var bug_url = tr.attr("data-bug_url");
        var influence = tr.attr("data-influence");

        // 回显多选框选中的选项
        var business = (tr.attr("data-business")).replace(/\ |\'|\[|]/g,'');
        var businessarr =business.split(",");
        var business_checkbox = $("input[name='business-box']");
        for(var i=0; i<businessarr.length; i++){
            $.each(business_checkbox,function(j, checkbox){
                var checkValue = $(checkbox).val();
                if(businessarr[i] == checkValue){
                    $(checkbox).attr('checked',true);
                }
            });
        }


        // 回显下拉菜单选中的选项
        var ishandled = tr.attr("data-ishandled");
        var ishandled_selector = document.getElementById("ishandled");
        for(var i=0;i<ishandled_selector.options.length;i++){
            if(ishandled_selector.options[i].value == ishandled){
                ishandled_selector.options[i].selected = true;
                break;
            }
        }
        var issue_type = tr.attr("data-issue_type");
        var issue_type_selector = document.getElementById("issue_type");
        for(var i=0;i<issue_type_selector.options.length;i++){
            if(issue_type_selector.options[i].value == issue_type){
                issue_type_selector.options[i].selected = true;
                break;
            }
        }
        var issue_lever = tr.attr("data-issue_lever");
        var issue_lever_selector = document.getElementById("issue_lever");
        for(var i=0;i<issue_lever_selector.options.length;i++){
            if(issue_lever_selector.options[i].value == issue_lever){
                issue_lever_selector.options[i].selected = true;
                break;
            }
        }
        var isBug = tr.attr("data-isBug");
        var isBug_selector = document.getElementById("isBug");
        for(var i=0;i<isBug_selector.options.length;i++){
            if(isBug_selector.options[i].value == isBug){
                isBug_selector.options[i].selected = true;
                break;
            }
        }


        var feedback_dateE = dialog.find("input[name='feedback_date']");
        var providerE = dialog.find("input[name='provider']");
        var issue_descE = dialog.find("textarea[name='issue_desc']");
        var reasonE = dialog.find("textarea[name='reason']");
        var solutionE = dialog.find("textarea[name='solution']");
        var reciverE = dialog.find("input[name='reciver']");
        var resolverE = dialog.find("input[name='resolver']");
        var remarkE = dialog.find("textarea[name='remark']");
        var image_urlE = dialog.find("input[name='image_url']");
        var bug_urlE = dialog.find("input[name='bug_url']");
        var influenceE = dialog.find("textarea[name='influence']");
        // var businessE = dialog.find("#business-line input:checked");
        var saveBtn = dialog.find("#save-feedback-btn");

        feedback_dateE.val(feedback_date);
        providerE.val(provider);
        issue_descE.val(issue_desc);
        reasonE.val(reason);
        solutionE.val(solution);
        reciverE.val(reciver);
        resolverE.val(resolver);
        remarkE.val(remark);
        image_urlE.val(image_url);
        bug_urlE.val(bug_url);
        influenceE.val(influence);
        // businessE.val(business);

        saveBtn.attr("data-type", "update"); // 给保存按钮绑定一个data-type属性，值为update
        saveBtn.attr("data-id", tr.attr("data-id"));


   });
});

// 查看
$(function () {
   $(".preview-feedback-btn").click(function (event) {
        var self = $(this);
        event.preventDefault();
        var dialog = $("#preview-feedback-dialog");
        dialog.modal("show");

        var tr = self.parent().parent();

        // 回显date，由于date存储的是时间格式，需要截取前面的日期进行回显
        var feedback_date = String(tr.attr("data-feedback_date")).substring(0,10);
        var provider = tr.attr("data-provider");
        var issue_desc = tr.attr("data-issue_desc");
        var reason = tr.attr("data-reason");
        var solution = tr.attr("data-solution");
        var reciver = tr.attr("data-reciver");
        var resolver = tr.attr("data-resolver");
        var remark = tr.attr("data-remark");
        var image_url = tr.attr("data-image_url");
        var bug_url = tr.attr("data-bug_url");
        var influence = tr.attr("data-influence");

        // 回显多选框选中的选项
        var business = (tr.attr("data-business")).replace(/\ |\'|\[|]/g,'');
        var businessarr =business.split(",");
        var business_checkbox = $("input[name='preview-business-box']");
        for(var i=0; i<businessarr.length; i++){
            $.each(business_checkbox,function(j, checkbox){
                var checkValue = $(checkbox).val();
                if(businessarr[i] == checkValue){
                    $(checkbox).attr('checked',true);
                }
            });
        }

        // 回显下拉菜单选中的选项
        var ishandled = tr.attr("data-ishandled");
        var ishandled_selector = document.getElementById("preview-ishandled");
        for(var i=0;i<ishandled_selector.options.length;i++){
            if(ishandled_selector.options[i].value == ishandled){
                ishandled_selector.options[i].selected = true;
                break;
            }
        }
        var issue_type = tr.attr("data-issue_type");
        var issue_type_selector = document.getElementById("preview-issue_type");
        for(var i=0;i<issue_type_selector.options.length;i++){
            if(issue_type_selector.options[i].value == issue_type){
                issue_type_selector.options[i].selected = true;
                break;
            }
        }
        var issue_lever = tr.attr("data-issue_lever");
        var issue_lever_selector = document.getElementById("preview-issue_lever");
        for(var i=0;i<issue_lever_selector.options.length;i++){
            if(issue_lever_selector.options[i].value == issue_lever){
                issue_lever_selector.options[i].selected = true;
                break;
            }
        }
        var isBug = tr.attr("data-isBug");
        var isBug_selector = document.getElementById("preview-isBug");
        for(var i=0;i<isBug_selector.options.length;i++){
            if(isBug_selector.options[i].value == isBug){
                isBug_selector.options[i].selected = true;
                break;
            }
        }


        var feedback_dateE = dialog.find("input[name='preview-feedback_date']");
        var providerE = dialog.find("input[name='preview-provider']");
        var issue_descE = dialog.find("textarea[name='preview-issue_desc']");
        var reasonE = dialog.find("textarea[name='preview-reason']");
        var solutionE = dialog.find("textarea[name='preview-solution']");
        var reciverE = dialog.find("input[name='preview-reciver']");
        var resolverE = dialog.find("input[name='preview-resolver']");
        var remarkE = dialog.find("textarea[name='preview-remark']");
        var image_urlE = dialog.find("input[name='preview-image_url']");
        var bug_urlE = dialog.find("input[name='preview-bug_url']");
        var influenceE = dialog.find("textarea[name='preview-influence']");
        feedback_dateE.val(feedback_date);
        providerE.val(provider);
        issue_descE.val(issue_desc);
        reasonE.val(reason);
        solutionE.val(solution);
        reciverE.val(reciver);
        resolverE.val(resolver);
        remarkE.val(remark);
        image_urlE.val(image_url);
        bug_urlE.val(bug_url);
        influenceE.val(influence);
   });
});

// 删除
$(function () {
    $(".delete-feedback-btn").click(function (event) {
        var self = $(this);
        var tr = self.parent().parent();
        var feedback_id = tr.attr('data-id');
        amanalert.alertConfirm({
            "msg":"确定删除这个轮播图吗？",
            "confirmCallback":function () {
                amanajax.post({
                    'url':'/delfeedback/',
                    'data':{
                        'feedback_id':feedback_id
                    },
                    'success':function (data) {
                        console.log(data['code']);
                        if(data['code'] == 1){
                            amanalert.alertSuccessToast('删除成功！');
                            window.location.reload();
                        }else {
                            amanalert.alertToast(data['message']);
                        }
                    },
                    'fail':function (error) {
                        amanalert.alertNetworkError();
                    }
                })
            }
        });
    });
});

// 新增时上传图片，图片服务器为七牛
$(function () {
    amanqiniu.setUp({
        'domain': 'http://q9jof2c2k.bkt.clouddn.com/',
        'browse_btn': 'upload-btn',
        'uptoken_url': '/common/uptoken/',
        'success': function (up,file,info) {
            var imageInput = $("input[name='image_url']");
            imageInput.val(file.name);
        },
        'error':function (up,err,errTip) {
            var errorMsg = err['message']
            if(errorMsg.indexOf('size error') != -1){
                amanalert.alertInfo('文件大小不能超过 1MB，请重新选择！');
            }else if(errorMsg.indexOf('extension error') != -1){
                amanalert.alertInfo('上传文件必须为常见的图片或视频格式，请重新选择！');
            }else {
                amanalert.alertInfo(errorMsg)
            }
        }
    });
});

// 字符较多时以tip或弹窗展示
$(function () {
  $('[data-toggle="popover"]').popover()
});
